const jwt = require('jsonwebtoken');
const { jwtConfig } = require('../config');
const roles = require('./role');

module.exports = {
  login(req, res) {
    if (req.body.username == 'admin') {
      const token = jwt.sign({
        user_name: req.body.username
      }, jwtConfig, { // 秘钥
        expiresIn: 60 * 60 // 过期时间
      });

      res.json({
        code: '200',
        errMsg: '登录成功',
        data: {
          userId: 1,
          username: req.body.username,
          token,
          routes: roles.adminRoles
        }
      });
    } else if (req.body.username == 'jack') {
      const token = jwt.sign({
        user_name: req.body.username
      }, jwtConfig, { // 秘钥
        expiresIn: 60 * 60 // 过期时间
      });

      res.json({
        code: '200',
        errMsg: '登录成功',
        data: {
          userId: 1,
          username: req.body.username,
          token,
          routes: roles.userRoles
        }
      });
    } else {
      res.json({
        code: '1001',
        errMsg: '账号密码错误'
      });
    }
  },
  logout(req, res) {
    res.json({
      code: '200',
      errMsg: '退出成功',
      data: {}
    })
  },
  forgetPassword(req, res) {
    const sql = ``;

    query(sql, [])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: {}
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  },
  changePassword(req, res) {
    const sql = ``;

    query(sql, [])
      .then(result => {
        res.json({
          code: '200',
          errMsg: '操作成功',
          data: {}
        })
      })
      .catch(err => {
        res.json({
          code: '1001',
          errorMsg: '操作失败',
          err: err
        })
      });
  }
}